/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package database.tables;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import database.DBManager;
import database.objects.DOMapResult;

/**
 *
 * @author Tri
 */
public class DTMapResult {
    private DB m_db;
    public DTMapResult() {
        m_db = DBManager.getInstance().getDB();
    }
    
    public DOMapResult getMapResult(String mapID, String userID) {
        DOMapResult mapResult = null;
        BasicDBObject object = new BasicDBObject();
        object.put(DOMapResult.KEY_MAP_ID, mapID);
        object.put(DOMapResult.KEY_USER_ID, userID);
        
        return mapResult;
    }
    
    public DBCollection getCollection() {
        return m_db.getCollection("");
    }
    
    public void insertMapResult(DOMapResult mapResult) {
        BasicDBObject dbObject = new BasicDBObject();
        dbObject.put(DOMapResult.KEY_MAP_ID, mapResult.getMapID());
        dbObject.put(DOMapResult.KEY_USER_ID, mapResult.getUserID());
        dbObject.put(DOMapResult.KEY_LEVEL, mapResult.getLevel());
        dbObject.put(DOMapResult.KEY_RATING, mapResult.getRating());
        dbObject.put(DOMapResult.KEY_SCORE, mapResult.getScore());
        getCollection().insert(dbObject);
    }
    
    public void updateMapResult(DOMapResult mapResult) {
        BasicDBObject searchQuery = new BasicDBObject();
        searchQuery.put(DOMapResult.KEY_MAP_ID, mapResult.getMapID());
        searchQuery.put(DOMapResult.KEY_USER_ID, mapResult.getUserID());
        
        BasicDBObject updateQuery = new BasicDBObject();
        updateQuery.put(DOMapResult.KEY_LEVEL, mapResult.getLevel());
        updateQuery.put(DOMapResult.KEY_RATING, mapResult.getRating());
        updateQuery.put(DOMapResult.KEY_SCORE, mapResult.getScore());
        
        getCollection().update(searchQuery, updateQuery);
    }
    
    public boolean deleteResult(String mapID, String userID) {
        BasicDBObject dbObject = new BasicDBObject();
        dbObject.put(DOMapResult.KEY_MAP_ID, mapID);
        dbObject.put(DOMapResult.KEY_USER_ID, userID);
        
        DBCursor cursor = getCollection().find(dbObject);
        if(cursor.count() > 0) {
            getCollection().remove(cursor.curr());
            return true;
        }
        return false;
    }
}
